using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using Common;
using Common.NotifyEvent;

namespace SSettings
{
	[Serializable]
	public class GridColumns : NotifyPropertyChanged
	{
		public const byte Info_Basic = 1;

		public const byte Info_Result = 2;

		public const byte Info_Patient = 4;

		public const string Key_CellName = "WellCell.Well.CellName";

		public const string Key_ProjectName = "WellCell.Well.Project.BasicInfo.Name";

		public const string Key_TubeName = "WellCell.Well.TubeName";

		public const string Key_TubeNo = "WellCell.Well.TubeNo";

		public const string Key_ChannelNo = "SampleTargetItem.ChannelNo";

		public const string Key_Dye = "SampleTargetItem.Dye";

		public const string Key_TargetName = "SampleTargetItem.TargetName";

		public const string Key_LineColor = "SampleTargetItem.LineColor";

		public const string Key_SampleType = "SampleTargetItem.SampleType";

		public const string Key_Property = "SampleTargetItem.Property";

		public const string Key_CompoundTubeNo = "WellCell.Well.Sample.CompoundTubeNo";

		public const string Key_TargetResultPrex = "SampleTargetItem.Result.";

		public const string Key_CtValue = "SampleTargetItem.Result.CtValue";

		public const string Key_CtMean = "SampleTargetItem.Result.CtMean";

		public const string Key_CtSD = "SampleTargetItem.Result.CtSD";

		public const string Key_CtCV = "SampleTargetItem.Result.CtCV";

		public const string Key_CnValue = "SampleTargetItem.Result.CnValue";

		public const string Key_CnMean = "SampleTargetItem.Result.CnMean";

		public const string Key_CnSD = "SampleTargetItem.Result.CnSD";

		public const string Key_CnCV = "SampleTargetItem.Result.CnCV";

		public const string Key_RnValue = "SampleTargetItem.Result.RnValue";

		public const string Key_MidValue = "SampleTargetItem.Result.MidValue";

		public const string Key_MidValueTarget = "SampleTargetItem.Result.MidValue";

		public const string Key_MidValueWell = "WellCell.Well.Result.MidValue";

		public const string Key_MidValueSample = "WellCell.Well.Sample.Result.MidValue";

		public const string Key_Tm = "SampleTargetItem.Result.Tm";

		public const string Key_Rm = "SampleTargetItem.Result.Rm";

		public const string Key_Wm = "SampleTargetItem.Result.Wm";

		public const string Key_Ym = "SampleTargetItem.Result.Ym";

		public const string Key_Geno = "SampleTargetItem.Result.Geno";

		public const string Key_QualityFactor = "SampleTargetItem.Result.QualityFactor";

		public const string Key_ADType = "WellCell.Well.Result.TypeNo";

		public const string Key_ADQualityFactor = "WellCell.Well.Result.QualityFactor";

		public const string Key_SampleConclusionList = "WellCell.Well.Sample.Result.ConclusionList";

		public const string Key_WellConclusionList = "WellCell.Well.Result.ConclusionList";

		public const string Key_TargetConclusionList = "SampleTargetItem.Result.ConclusionList";

		public const string Key_PatientInfoPrex = "WellCell.Well.Sample.";

		public const string Key_PatientInfoID = "WellCell.Well.Sample.PatientInfo.ID";

		public const string Key_PatientInfoName = "WellCell.Well.Sample.PatientInfo.Name";

		public const string Key_PatientInfoSex = "WellCell.Well.Sample.PatientInfo.Sex";

		public const string Key_PatientInfoAge = "WellCell.Well.Sample.PatientInfo.Age";

		public const string Key_PatientInfoSpecimen = "WellCell.Well.Sample.PatientInfo.Specimen";

		public const string Key_PatientInfoSpecimenDate = "WellCell.Well.Sample.PatientInfo.SpecimenDate";

		public const string Key_PatientInfoDoctor = "WellCell.Well.Sample.PatientInfo.Doctor";

		public const string Key_PatientInfoOffice = "WellCell.Well.Sample.PatientInfo.Office";

		public const string Key_PatientInfoDiagnosis = "WellCell.Well.Sample.PatientInfo.Diagnosis";

		public const string Key_PatientInfoCaseID = "WellCell.Well.Sample.PatientInfo.CaseID";

		public const string Key_PatientInfoBedID = "WellCell.Well.Sample.PatientInfo.BedID";

		public const string Key_PatientInfoInPatientID = "WellCell.Well.Sample.PatientInfo.InPatientID";

		public const string Key_PatientInfoOutPatientID = "WellCell.Well.Sample.PatientInfo.OutPatientID";

		public const string Key_PatientInfoRemark = "WellCell.Well.Sample.PatientInfo.Remark";

		public const string Key_PatientInfoCustom = "WellCell.Well.Sample.PatientInfoCustomData.";

		public const string Key_SampleName = "WellCell.Well.Sample.SampleName";

		private bool _NotDisplayNullColumn;

		protected ObservableCollection<ColumnInfo> _Columns = new ObservableCollection<ColumnInfo>();

		public bool NotDisplayNullColumn
		{
			get
			{
				return _NotDisplayNullColumn;
			}
			set
			{
				if (_NotDisplayNullColumn != value)
				{
					_NotDisplayNullColumn = value;
					OnPropertyChanged("NotDisplayNullColumn");
				}
			}
		}

		public ObservableCollection<ColumnInfo> Columns
		{
			get
			{
				return _Columns;
			}
			set
			{
				if (_Columns != value)
				{
					_Columns = value;
					OnPropertyChanged("Columns");
				}
			}
		}

		public virtual void SetVisibilityDefault()
		{
			NotDisplayNullColumn = false;
		}

		protected virtual GridColumns CreateColumns()
		{
			return new GridColumns();
		}

		public GridColumns Clone()
		{
			GridColumns gridColumns = CreateColumns();
			gridColumns.Columns.Clear();
			gridColumns.NotDisplayNullColumn = NotDisplayNullColumn;
			for (int i = 0; i < _Columns.Count; i++)
			{
				gridColumns.Columns.Add(_Columns[i].Clone());
			}
			return gridColumns;
		}

		public virtual void SaveToStream(StreamEx stream)
		{
			stream.Write(PropertyKey.Key_GridColumns);
			List<PropertyKey> list = new List<PropertyKey>();
			List<byte[]> list2 = new List<byte[]>();
			list.Add(PropertyKey.Key_Items);
			StreamEx streamEx = new StreamEx();
			streamEx.Write(BytesConverter.GetBytes(Columns.Count));
			foreach (ColumnInfo column in Columns)
			{
				streamEx.Write((column.FieldName == null) ? string.Empty : column.FieldName);
				column.SaveToStream(streamEx);
			}
			streamEx.Seek(0L, SeekOrigin.Begin);
			list2.Add(streamEx.ToArray());
			streamEx.Close();
			stream.Write(PropertyKey.Key_Index);
			stream.WriteIndex(list, list2);
			stream.Flush();
		}

		public virtual bool ReadFromStream(StreamEx stream)
		{
			SetVisibilityDefault();
			PropertyKey propertyKey = stream.ReadKey();
			if (propertyKey == PropertyKey.Key_GridColumns)
			{
				propertyKey = stream.ReadKey();
				if (propertyKey == PropertyKey.Key_Index)
				{
					List<PropertyKey> keys;
					List<byte[]> dates;
					stream.ReadIndex(out keys, out dates);
					bool flag = true;
					for (int i = 0; i < keys.Count; i++)
					{
						if (!flag)
						{
							return false;
						}
						PropertyKey propertyKey2 = keys[i];
						if (propertyKey2 != PropertyKey.Key_Items)
						{
							continue;
						}
						StreamEx streamEx = new StreamEx();
						streamEx.Write(dates[i]);
						streamEx.Seek(0L, SeekOrigin.Begin);
						int num = streamEx.ReadInt();
						for (int j = 0; j < num; j++)
						{
							ColumnInfo columnInfo = new ColumnInfo();
							string FieldName = streamEx.ReadString();
							columnInfo.ReadFromStream(streamEx);
							if (!string.IsNullOrEmpty(FieldName))
							{
								ColumnInfo columnInfo2 = _Columns.Where((ColumnInfo s) => s.FieldName == FieldName).FirstOrDefault();
								if (columnInfo2 != null)
								{
									columnInfo2.Visibility = columnInfo.Visibility;
									columnInfo2.DataSource = columnInfo.DataSource;
									if (_Columns.Count > j)
									{
										_Columns.Move(_Columns.IndexOf(columnInfo2), j);
									}
								}
							}
							else
							{
								Columns.Insert(j, columnInfo);
							}
						}
						streamEx.Close();
					}
				}
			}
			else
			{
				stream.Seek(-4L, SeekOrigin.Current);
				int num2 = stream.ReadInt();
				for (int k = 0; k < num2; k++)
				{
					ColumnInfo columnInfo3 = new ColumnInfo();
					columnInfo3.ReadFromStream(stream);
					if (k < _Columns.Count && columnInfo3.AllowDelete)
					{
						_Columns[k].Visibility = columnInfo3.Visibility;
					}
					else
					{
						Columns.Add(columnInfo3);
					}
				}
				propertyKey = stream.ReadKey();
				if (propertyKey == PropertyKey.Key_NotDisplayNullColumn)
				{
					stream.ReadBool();
					NotDisplayNullColumn = false;
				}
				else
				{
					stream.Seek(-4L, SeekOrigin.Current);
				}
			}
			return true;
		}
	}
}
